package com.aier.common.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.aier.admin.entity.AdminUser;
import com.aier.admin.service.IAdminUserService;
import com.aier.common.dao.ICommonDAO;
import com.aier.common.service.IFeedbackService;
import com.aier.proxy.service.IProxyService;
import com.aier.user.entity.Feedback;
import com.aier.user.entity.FeedbackContent;
import com.aier.user.entity.mapper.FeedbackContentRowMapper;
import com.aier.user.entity.mapper.FeedbackRowMapper;

@Component("feedbackService")
public class FeedbackServiceImpl_J implements IFeedbackService {

    @Resource(name = "commonDao")
    private ICommonDAO commonDao;

    public void setCommonDao(ICommonDAO commonDao) {
        this.commonDao = commonDao;
    }
    
    @Resource(name = "adminUserService")
    private IAdminUserService adminUserService;

    public void setAdminUserService(IAdminUserService adminUserService) {
        this.adminUserService = adminUserService;
    }

    @Override
    public Map<String, Object> searchFeedbackByUid(int page, int page_size,
            Long uid, int sortable) {
        String sql;
        if (null == uid) {
            sql = "select fb.*,ui.realname as name from feedback as fb LEFT JOIN admin_user a " +
                    "on a.aid=fb.`handler` LEFT JOIN user_info ui ON ui.uid=a.uid";
        } else {
            sql = "select fb.*,ui.realname as name from feedback as fb LEFT JOIN admin_user a " +
                    "on a.aid=fb.`handler` LEFT JOIN user_info ui ON ui.uid=a.uid where fb.uid=?";
        }
        switch (sortable) {
            case IProxyService.ENTRANCE_SORTABLE_SN:
                sql += " order by fb.cdate";
                break;
            case IProxyService.ENTRANCE_SORTABLE_SN_DESC:
                sql += " order by fb.cdate desc";
                break;
        }
        if (null == uid) {
            return commonDao.queryPage(sql,
                    page, page_size, new Object[] {},
                    new FeedbackRowMapper());
        }
        return commonDao.queryPage(sql,
                page, page_size, new Object[] {uid},
                new FeedbackRowMapper());
    }

    @Override
    public int updateFeedbackByid(Short score, Long fbid) {
        if (commonDao.update("update feedback set score=? where fbid=?",
                new Object[] {score, fbid }) > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int insertFeedback(Long uid, String title, Timestamp cdate, String content) {
        if (commonDao.update("insert into feedback(title,cdate,uid,content) value(?,?,?,?)",
                new Object[] {title,cdate,uid,content})>0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int insertFeedbackContent(Long fbid, String content,
            Timestamp cdate, Long uid, Short state, Long mid) {
        
        if (commonDao.update("insert into feedback_content(fbid,cdate,uid,content) value(?,?,?,?)",
                new Object[] {fbid,cdate,uid,content})>0) {
            
            // 判断用户是维护人员还是其他用户
            //Customer customer = customerService.searchCustomerByUid(uid);
            AdminUser adminUser = adminUserService.searchAdminUser(uid);
            // 普通用户
            if (null == adminUser) {
                if (state == 2) {
                    commonDao.update("update feedback set state=1 where fbid=?",
                            new Object[] {fbid });
                }
            } else {
                // 维护人员
                if (null == mid || mid == 0) {
                    commonDao.update("update feedback set state=2,handler=? where fbid=?",
                            new Object[] {adminUser.getAid(),fbid });
                } else {
                    commonDao.update("update feedback set state=2 where fbid=?",
                            new Object[] {fbid });
                }
                    
            }
            return 1;
        } else {
            return 0;
        }
        
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<FeedbackContent> searchContent(Long fbid) {
        return (List<FeedbackContent>) commonDao.list("select fc.*,ui.realname as `name`,u.account as account from feedback_content fc " +
        		"LEFT JOIN `user` u ON u.uid=fc.uid LEFT JOIN user_info ui on fc.uid=ui.uid where fc.fbid=? order by fc.cdate asc",
                new Object[] {fbid}, new FeedbackContentRowMapper());
    }

    @Override
    public String searchRealname(Long uid) {
        String realname = (String) commonDao.listByObject("select realname from user_info where uid=?",
            new Object[] { uid }, new RowMapper<String>() {
                @Override
                public String mapRow(ResultSet rs, int arg1)
                    throws SQLException {
                    return rs.getString("realname");
                }
            });
        if (null == realname || "".equals(realname)) {
            return (String) commonDao.listByObject(
                    "select account from user where uid=?",
                    new Object[] { uid }, new RowMapper<String>() {
                        @Override
                        public String mapRow(ResultSet rs, int arg1)
                            throws SQLException {
                            return rs.getString("account");
                        }
                    });
        } else {
            return realname;
        }
    }

    @Override
    public Feedback searchFeedbackByFbid(Long fbid) {
        return (Feedback) commonDao.listByObject("select * from feedback where fbid=?", 
                new Object[] {fbid }, new FeedbackRowMapper());
    }
    
}
